package com.wrk._3_方法._02_习题;

import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;

public class _009_双色球 {
    /**投注号码由6个红色球号码和1个蓝色球号码组成,红色球号码从1-33中选择；蓝色球号码从1-16中选择。*/
    public static void main(String[] args) {
        //用户输入号码
        int [] userNumber =  userNumberArray();

        //获取中奖号码
        int [] winningNumbers = winningNumbersArray(userNumber.length);
        System.out.print("中奖号码为：");
        for (int i = 0; i < winningNumbers.length; i++) {
            System.out.print(winningNumbers[i]+" ");
        }
        System.out.println();

        //调用方法，获取中奖结果
        winningResults(userNumber,winningNumbers);
    }
    /**用户输入号码*/
    public static int [] userNumberArray(){
        //用户输入自己买的号码，转换为数组
        Scanner scanner = new Scanner(System.in);
        int [] userNumber = new int[7];
        //用户输入
        for (int i = 0; i < userNumber.length -1; ) {
            System.out.print("请输入第"+(i+1)+"个红球号码：");
            int ballNumber = scanner.nextInt();
            //红球号码范围校验
            if (ballNumber >=1 && ballNumber <= 33){
                //判断红球号码是否重复
                boolean fale = contains(userNumber,ballNumber);
                if (!fale){
                    userNumber[i] = ballNumber;
                    i++;
                }else {
                    System.out.println("红球号码重复，请重新输入");
                }
            }else {
                System.out.println("红球号码值超出范围，请重新输入");
            }
        }
        //篮球号码范围校验
        while (true){
            System.out.print("请输入第"+userNumber.length+"个蓝球号码：");
            int index2 = scanner.nextInt();
            if (index2 >=1 && index2 <= 16){
                userNumber[userNumber.length -1] = index2;
                break;
            }else {
                System.out.println("篮球号码值超出范围，请重新输入");
            }
        }
        return userNumber;
    }

    /**生成默认中奖号码*/
    public static int[] winningNumbersArray(int length){
        //根据规则，随机生成一个号码，作为中奖号码，以数组形式表示
        int [] winningNumbers = new int[length];
        //生成红球号码
        for (int i = 0; i < winningNumbers.length -1; ) {
            int index = new Random().nextInt(1,34);
            boolean fale = contains(winningNumbers,index);
            //判断是否重复
            if (!fale){
                winningNumbers[i] = index;
                i++;
            }
        }
        //生成蓝球号码
        winningNumbers[winningNumbers.length -1] = new Random().nextInt(1,17);

        return winningNumbers;
    }
    /**判断红球号码是否重复*/
    public static boolean contains(int [] winningNumbers,int index){
        for (int i = 0; i < winningNumbers.length; i++) {
            if (winningNumbers[i] == index){
                return true;
            }
        }
        return false;
    }

    /**计算红球、篮球数,判断获得几等奖*/
    public static void winningResults (int [] userNumber,int [] winningNumbers){
        int redBall = 0;
        int blueBall = 0;
        for (int i = 0; i < userNumber.length -1; i++) {
            for (int j = 0; j < winningNumbers.length -1; j++) {
                //计算红球数，!used[j]表示当前元素是false
                if (userNumber[i] == winningNumbers[j]){
                    redBall++;
                    break;
                }
            }
        }
        //计算篮球数
        if (userNumber[userNumber.length -1] == winningNumbers[winningNumbers.length -1]){
            blueBall++;
        }
        //判断几等奖
        if (redBall == 6 && blueBall == 1){
            System.out.println("恭喜你获得一等奖，奖金1000万");
        }
        else if (redBall == 6 && blueBall == 0){
            System.out.println("恭喜你获得二等奖，奖金500万");
        }
        else if (redBall == 5 && blueBall == 1){
            System.out.println("恭喜你获得三等奖，奖金3000元");
        }
        else if ((redBall == 5 && blueBall == 0) || (redBall == 4 && blueBall == 1)){
            System.out.println("恭喜你获得四等奖，奖金200元");
        }
        else if ((redBall == 4 && blueBall == 0) || ((redBall == 3 && blueBall == 1))){
            System.out.println("恭喜你获得五等奖，奖金10元");
        }
        else if ((redBall == 2 && blueBall == 1) || (redBall == 1 && blueBall == 1) || (redBall == 0 && blueBall == 1)){
            System.out.println("恭喜你获得六等奖，奖金5元");
        }else {
            System.out.println("您未中奖");
        }
    }
}